# -*- coding: utf-8 -*-
""" This file defines data structure types.
"""
from .ir_type import IType
from .ir_value import IValue, IValueInt


# -- List --
class ITypeList(IType):
    def __init__(self, managed_type: IType):
        super(ITypeList, self).__init__('list', -1)
        self.managed_type = managed_type

    def __str__(self):
        return 'List<{}>'.format(str(self.managed_type))


class IValueList(IValue):
    def __init__(self, managed_type):
        super(IValueList, self).__init__(ITypeList(managed_type), [])

    def __str__(self):
        return '[{}]'.format(', '.join(str(x) for x in self.value))


# -- Struct (Dict) --
class ITypeStruct(IType):
    def __init__(self, name: str, members: list, member_types: list,
                 extract: bool = False):
        super(ITypeStruct, self).__init__('struct', -1)
        self.name = name
        self.member = members
        self.member_type = member_types
        self.extract = extract

    def __str__(self):
        return 'struct<{}>'.format(self.name)


class IValueStruct(IValue):
    def __init__(self, type: ITypeStruct):
        super(IValueStruct, self).__init__(type, {})

    def __str__(self):
        return str(self.value)


class IValueDict(IValue):
    """ Dict is used for those dictionary constants.
    """
    def __init__(self):
        super(IValueDict, self).__init__(IType('dict'), {})

    def __str__(self):
        return '{' + ', '.join('{}: {}'.format(vkey, str(self.value[vkey])) for vkey in self.value) + '}'


# -- Enum --
class ITypeEnum(IType):
    def __init__(self, name: str):
        super(ITypeEnum, self).__init__('enum', 32)
        self.name = name
        self.consts = {}

    def get(self, item: str):
        if item in self.consts:
            return IValueInt(self.consts[item])

    def __str__(self):
        return self.name